package com.wbl.train.business.controller.admin;
import cn.hutool.core.util.ObjectUtil;
import com.wbl.train.business.service.TrainSeatService;
import com.wbl.train.common.business.dto.TrainSeatDto;
import com.wbl.train.common.business.entity.TrainSeatEntity;
import com.wbl.train.common.exception.ApplicationException;
import com.wbl.train.common.page.PageVo;
import com.wbl.train.common.result.R;
import com.wbl.train.common.result.ResultCode;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 王博纶
 * @since 2024-02-02
 * @description 座位
 */
@AllArgsConstructor
@Slf4j
@RestController
@RequestMapping("/admin/train-seat")
@Tag(name = "座位控制器类",
        description = "TrainSeatController 座位 后端数据接口")
public class TrainSeatAdminController {
    private final TrainSeatService trainSeatService;


    /**
     * 通过id查询 座位
     * @param trainSeatDto trainSeatDto
     * @return Result
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/query")
    public R<PageVo<TrainSeatEntity>> getById(TrainSeatDto trainSeatDto) {
        // 火车车厢座位查询
        log.debug("trainSeatDto :{}",trainSeatDto);
        PageVo<TrainSeatEntity> pageVo = trainSeatService.queryTrainSeat(trainSeatDto);
        return R.ok("查询成功",pageVo);
    }

    /**
     * 新增 座位
     * @param trainSeat 座位
     * @return Result
     */
    @Operation(summary = "新增座位", description = "新增座位")
    @PostMapping("/save")
    public R<Boolean> save(@Valid @RequestBody TrainSeatEntity trainSeat) {
        return R.ok("保存成功",trainSeatService.insertAndUpdateTrain(trainSeat));
    }


    /**
     * 通过id删除 座位
     * @param id id
     * @return Result
     */
    @Operation(summary = "通过id删除座位",
               description = "通过id删除座位")
    @DeleteMapping("/delete/{id}")
    public R<Boolean> removeById(@PathVariable Long id) {
        if(ObjectUtil.isNull(trainSeatService.getById(id))){
            throw new ApplicationException(ResultCode.DATA_NOT_EXIST,"当前车厢座位信息不存在");
        }
        return R.ok(trainSeatService.removeById(id));
    }

}
